package com.propertyportal.alladapter;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.propertyportal.manageproperty.R;
import com.propertyportal.propertylist.ImageLoader;
import com.propertyportal.propertylistfragment.PropertyHelper;
import com.propertyportal.propertylistfragment.PropertyList.OnClickSelectedListener;

public class PropertyListAdapter extends BaseAdapter {

	// Declare Variables
	Context mContext;
	LayoutInflater inflater;
	private List<PropertyHelper> worldpopulationlist = null;
	private ArrayList<PropertyHelper> arraylist;
	private OnClickSelectedListener listener;
	private ImageLoader imageloader;
	private View vi;
	
	private Locale ph=new Locale("en","Philippines");
	private NumberFormat defaultFormat = NumberFormat.getCurrencyInstance(ph);
	public PropertyListAdapter(){
		
	}
	public PropertyListAdapter(Context context, List<PropertyHelper> worldpopulationlist) {
		mContext = context;
		this.worldpopulationlist = worldpopulationlist;
		//this.location=location;
		inflater = LayoutInflater.from(mContext);
		this.arraylist = new ArrayList<PropertyHelper>();
		this.arraylist.addAll(worldpopulationlist);
		listener=(OnClickSelectedListener)mContext;
		vi=new View(context);
		imageloader=new ImageLoader(mContext);
	}

	public class ViewHolder {
		TextView rank;
		TextView country;
		TextView population;
		ImageView image;
		ImageView edit;
		ImageView delete;
		ImageView insert_image;
	}

	@Override
	public int getCount() {
		return worldpopulationlist.size();
	}

	@Override
	public PropertyHelper getItem(int position) {
		return worldpopulationlist.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public View getView(final int position,View view, ViewGroup parent) {
		final ViewHolder holder;
		
		if (view == null) {
			holder = new ViewHolder();
			view = inflater.inflate(R.layout.listcontent, null);
			// Locate the TextViews in listview_item.xml
			holder.rank = (TextView) view.findViewById(R.id.title);
			holder.country = (TextView) view.findViewById(R.id.price);
			holder.population = (TextView) view.findViewById(R.id.address);
			holder.image=(ImageView)view.findViewById(R.id.list_image);
			holder.edit=(ImageView)view.findViewById(R.id.edit_button);
			holder.delete=(ImageView)view.findViewById(R.id.delete_button);
			holder.insert_image=(ImageView)view.findViewById(R.id.insert_image);
			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}
		Double presyo=0.00;
		try{
			presyo=Double.parseDouble(worldpopulationlist.get(position).getPrice());	
		}catch(NumberFormatException num){

		}
		holder.rank.setText(worldpopulationlist.get(position).getName());
		holder.country.setText(""+defaultFormat.format(presyo));
		holder.population.setText(worldpopulationlist.get(position).getAddress());	
		imageloader.DisplayImage("/Avalon/jorja_.jpg", holder.image);
		holder.edit.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
			listener.onEditSelected(v, position);
			}
		});
		
		holder.delete.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				listener.onClickDelete(v, position);
			}
		});
		holder.insert_image.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
			listener.onInsertImage(v, position);
			}
		});
		// Listen for ListView Item Click
		 view.setOnClickListener(new OnClickListener() {
				
				public void onClick(View v) {
					// TODO Auto-generated method stub
					listener.onArticleSelected(v, position);
					vi.setSelected(false);
					v.setSelected(true);
					vi=v;
					
				}
			});
		 

		return view;
	}

	// Filter Class
	public void filter(String charText) {
			
		charText = charText.toLowerCase(Locale.getDefault());
		worldpopulationlist.clear();

		if (charText.length() == 0) {
			worldpopulationlist.addAll(arraylist);
		} 
		else 
		{
			for (PropertyHelper wp : arraylist) 
			{
				if (wp.getName().toLowerCase(Locale.getDefault()).contains(charText)) 
				{
					worldpopulationlist.add(wp);
					
				}
				
			}
		}
		notifyDataSetChanged();

	}

}